import { loginUser } from '@/api/user'
import { delToken, getToken, setToken } from '@/utils/storage'

export default {
  namespaced: true,
  state: {
    token: getToken() || ''
  },
  getters: {
  },
  mutations: {
    updateToken(state, token) {
      // 存在本地和state中
      state.token = token
      setToken(token)
    },
    // 清除token
    clearToken(state) {
      state.token = ''
      delToken()
    }
  },
  actions: {
    // 请求数据，返回token，存token
    async loginArticle(context, data) {
      const res = await loginUser(data)
      // console.log(res.data.token)
      context.commit('updateToken', res.data.token)
    }
  },
  modules: {
  }
}
